package GameSrc;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;

public class Net extends GameObject {
    private final int speed = 16;

    private final double angle;

    private boolean isExplosion = false;

    private int num=1;

    private final String imgHead = "Game/img/net/net";
    private final String tail = ".png";

    private Fish catchFish;



    public Net( int x, int y, GameWindow gameWindow) {
        super(20,35, x, y, "Game/img/net/net1.png", gameWindow);
        angle = this.getGameWindow().getTan();
    }



    public void move(){
        if (!isExplosion){
            int spX = -(int)(speed*Math.sin(angle));
            int spY = (int)(speed*Math.cos(angle));

            int x = this.getX();
            int y = this.getY();
            this.setX(x-spX);
            this.setY(y-spY);
            this.renewalRec(this.getX(),this.getY(),20,35);
        }

    }

    public void isOutOfBounds(){
        if ((this.getX()<10||this.getX()>1000)||(this.getY()<10||this.getY()>672)){
            isExplosion=true;
        }
    }

    public void isCatch(){
        ArrayList<Fish> fish=this.getGameWindow().fish;
        if (fish!=null){
            for (int i = 0; i < fish.size(); i++) {
                if (fish.get(i).getRectangle().intersects(this.getRectangle())) {
                    this.isExplosion=true;
                    this.getGameWindow().fish.get(i).setExplosion(true);
                    catchFish=this.getGameWindow().fish.get(i);
                    this.getGameWindow().addScare(catchFish);
                }
            }
        }
    }

    public void paintNet(Graphics g){
        if (isExplosion){
            if (num<12){
                this.setImg(imgHead+num+tail);
                num++;
            }else if (num==12){
                this.setImg(imgHead+num+tail);
                this.getGameWindow().removeNet.add(this);
                this.getGameWindow().removeFish.add(catchFish);
            }
        }
        AffineTransform old = ((Graphics2D) g).getTransform(); // 保存原始变换
        ((Graphics2D) g).rotate(angle, this.getX() + 10, this.getY() + 17); // 应用炮筒的角度
        g.drawImage(this.getImg(), this.getX(), this.getY(), null);
        ((Graphics2D) g).setTransform(old); // 恢复原始变换
    }
}
